package com.liuhaoqi.raletiveblog.service.Impl;

import com.liuhaoqi.raletiveblog.entity.Comment;
import com.liuhaoqi.raletiveblog.entity.dto.CommentDto;
import com.liuhaoqi.raletiveblog.mapper.CommentMapper;
import com.liuhaoqi.raletiveblog.result.business.PageResult.CommentMangPageResult;
import com.liuhaoqi.raletiveblog.service.CommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/4/25 22:03
 * @Version 1.0
 */
@Service
public class CommentsServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;


    @Override
    public int getCommentsnum2() {
        return commentMapper.getCommentsnum2();
    }

    @Override
    public int getCommentsnum1() {
        return commentMapper.getCommentsnum1();
    }

    @Override
    public int updateCommentByCommentId(Comment comment) {
        return commentMapper.updateCommentByCommentId(comment);
    }

    @Override
    public int deleteCommentByCommentId(Long commentId) {
        return commentMapper.deleteCommentByCommentId(commentId);
    }

    @Override
    public List<CommentMangPageResult> getCommentsMang(int start,int pageSize) {
        return commentMapper.getCommentsMang(start,pageSize);
    }

    @Override
    public List<CommentMangPageResult> getCommentsMangTest(int start, int end, int pagesize) {
        return commentMapper.getCommentsMangTest(start,end,pagesize);
    }

    @Override
    public int getCommentsMangList() {
        return commentMapper.getCommentsMangList();
    }

    @Override
    public int insertCommet(Comment comment) {
        return commentMapper.saveComment(comment);
    }
    //存放迭代找出的所有子代的集合
    private List<CommentDto> tempReplys = new ArrayList<>();

    @Override
    public List<CommentDto> getCommentsList(Long blogId) {
        //查出父节点
        List<CommentDto> commentDtos = commentMapper.findByParentIdNull(blogId, Long.parseLong("-1"));
        for (CommentDto commentDto : commentDtos) {
            Long id=commentDto.getCommentid();
            String parentNickname1= commentDto.getNickname();
            List<CommentDto> childComments = commentMapper.findByParentIdNotNull(id);
            //查询出子评论
            combineChildren(childComments, parentNickname1);
            commentDto.setReplies(tempReplys);
            tempReplys = new ArrayList<>();
        }
        return commentDtos;
    }

    /**
     * @Description: 查询出子评论
     * @Auther: ONESTAR
     * @Date: 17:31 2020/4/14
     * @Param: childComments：所有子评论
     * @Param: parentNickname1：父评论的姓名
     * @Return:
     */
    private void combineChildren(List<CommentDto> childComments, String parentNickname1) {
        //判断是否有一级子回复
        if(childComments.size() > 0){
            //循环找出子评论的id
            for(CommentDto childComment : childComments){
                String parentNickname = childComment.getNickname();
                childComment.setParentNickname(parentNickname1);
                tempReplys.add(childComment);
                Long childId = childComment.getCommentid();
                //查询二级以及所有子集回复
                recursively(childId, parentNickname);
            }
        }
    }
    /**
     * @Description: 循环迭代找出子集回复
     * @Auther: ONESTAR
     * @Date: 17:33 2020/4/14
     * @Param: childId：子评论的id
     * @Param: parentNickname1：子评论的姓名
     * @Return:
     */
    private void recursively(Long childId, String parentNickname1) {
        //根据子一级评论的id找到子二级评论
        List<CommentDto> replayComments = commentMapper.findByReplayId(childId);

        if(replayComments.size() > 0){
            for(CommentDto replayComment : replayComments){
                String parentNickname = replayComment.getNickname();
                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getCommentid();
                tempReplys.add(replayComment);
                //循环迭代找出子集回复
                recursively(replayId,parentNickname);
            }
        }
    }

}
